package controller;
import model.Map;

import java.awt.Graphics;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

public class MapManager
{
        private ArrayList<Map> maps = new ArrayList<Map>();
        private int mapCount;
        private int currentIndex;
        private Map currentMap;
        
        public MapManager()
        {
                mapCount = 0;
                currentIndex = 0;
                currentMap = null;
        }
        
        public MapManager(String file) throws Exception
        {
                FileInputStream fileIn = new FileInputStream(file);
                ObjectInputStream inStream = new ObjectInputStream(fileIn);
                
                maps = (ArrayList<Map>)inStream.readObject();
                
                mapCount = maps.size();
                currentIndex = 0;
                currentMap = null;
                
                inStream.close();               
        }
        
        public void load(String file) throws Exception
        {
                FileInputStream fileIn = new FileInputStream(file);
                ObjectInputStream inStream = new ObjectInputStream(fileIn);
                
                maps = (ArrayList<Map>)inStream.readObject();
                
                mapCount = maps.size();
                currentIndex = 0;
                currentMap = null;
                
                inStream.close();
        }
        
        public void save(String file) throws Exception
        {
                FileOutputStream fileOut = new FileOutputStream(file);
                ObjectOutputStream outStream = new ObjectOutputStream(fileOut);
                
                outStream.writeObject(maps);
                
                outStream.flush();
                outStream.close();
        }
        
        public void addMap(Map map)
        {
                maps.add(map);
                mapCount++;
                currentIndex = 0;
                currentMap = null;
        }
        
        public void setCurrentMap(int index)
        {
                currentMap = maps.get(index);
                currentIndex = index;
        }
        
        public int getCurrentIndex()
        {
                return currentIndex;
        }
        
        public int getMapCount()
        {
                return mapCount;
        }
        
        public void update(int input)
        {
                currentMap.update(input);
        }
        
        public void drawCurrentMap(Graphics page)
        {
                currentMap.drawObjects(page);
        }

}
